/**
 * Copyright (C) 2012 Oleg Anastassov <o.anastasov@gmail.com>
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kz.kstu.ent.business.client.config;

import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

/**
 * @author Oleg Anastassov
 */
public final class Configuration {
    private static final Logger LOGGER = Logger.getLogger(Configuration.class);

    public static final String HOST_PARAM_NAME = "host";
    public static final String PORT_PARAM_NAME = "port";
    public static final String MODE_PARAM_NAME = "mode";
    public static final String DEBUG_MODE_PARAM_VALUE = "debug";
    public static final String CONFIG_FILE_PATH = "conf/config.properties";

    private Properties properties = new Properties();
    private static Configuration instance;

    private Configuration() {
    }

    public static Configuration getInstance() {
        if (instance == null) {
            instance = new Configuration();
            instance.readProperties();
        }
        return instance;
    }

    public String getParameter(String paramName) {
        return properties.getProperty(paramName);
    }

    private void readProperties() {
        try {
            File propertiesFile = new File(getAppPathDir(), CONFIG_FILE_PATH);
            properties.load(new FileInputStream(propertiesFile));
        } catch (FileNotFoundException e) {
            LOGGER.error("Configuration file not found, file path = " + CONFIG_FILE_PATH, e);
        } catch (IOException e) {
            LOGGER.error("Can not continueRead configuration file, file path = " + CONFIG_FILE_PATH, e);
        }
    }

    public String getAppPathDir() {
        URL jarUrl = Configuration.class.getProtectionDomain().getCodeSource().getLocation();
        File jar = new File(jarUrl.getFile());
        File jarDir = jar.getParentFile();
        File applicationDir = jarDir.getParentFile();
        return applicationDir.getPath();
    }

    public boolean isDebugMode() {
        String modeValue = getParameter(MODE_PARAM_NAME);
        return Configuration.DEBUG_MODE_PARAM_VALUE.equalsIgnoreCase(modeValue);
    }
}
